//
//  FQDNetworkTool.m
//  FakeQDaily
//
//  Created by mewxiv on 8/19/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDNetworkTool.h"
#import "QDailyAPIConstants.h"
#import "AFNetworking.h"

#import "MBProgressHUD+FQDExtension.h"

@interface FQDNetworkTool ()

@property (nonatomic, strong) AFNetworkReachabilityManager *afReachabilityManager;
@property (nonatomic, assign) AFNetworkReachabilityStatus afNetworkStatus;

@end

@implementation FQDNetworkTool

+ (instancetype)sharedInstance
{
    static FQDNetworkTool *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self class] new];
    });
    return instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupReachabilityManager];
    }
    return self;
}

- (void)setupReachabilityManager
{
    _afReachabilityManager = [AFNetworkReachabilityManager sharedManager];
    [_afReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
    }];
}

- (void)startMonitorNetwork
{
    [_afReachabilityManager startMonitoring];
}

- (BOOL)isNetworkReachable
{
    // 未检测网络状态前，默认可联网
    AFNetworkReachabilityStatus status = self.afReachabilityManager.networkReachabilityStatus;
    BOOL isReachable = (status != AFNetworkReachabilityStatusNotReachable);
    return isReachable;
}

- (void)dealloc
{
    [_afReachabilityManager stopMonitoring];
}

@end
